<?php
namespace app\api\controller\system;

use app\common\controller\AdminController;
use think\captcha\facade\Captcha;
use think\facade\Env;
use think\facade\Db;
use think\App;
use app\admin\model\SystemConfig;

/**
 * Class Login
 *
 * @package app\admin\controller
 */
class OperationRecord extends AdminController
{
    
    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);
        
        $this->model = new \app\admin\model\SystemOperationRecord();
    }

    /**
     * 用户信息
     *
     * @return mixed
     */
    public function index()
    {
        $request = $this->request;
        $tenant_id = $request->_TENANTID;
        
        $page = input("page");
        $limit = input("limit");
        
        $where[] = [
            "system_operation_record.tenant_id",
            "=",
            $tenant_id
        ];
        
        if (! empty(input('username'))) {
            $where[] = [
                "user.username",
                "LIKE",
                "%" . input('username') . "%"
            ];
        }
        if (! empty(input('module'))) {
            $where[] = [
                "system_operation_record.module",
                "LIKE",
                "%" . input('module') . "%"
            ];
        }
        if (! empty(input('createTimeStart'))) {
            $where[] = [
                "create_time",
                ">=",
                strtotime(input('createTimeStart'))
            ];
        }
        
        if (! empty(input('createTimeEnd'))) {
            $where[] = [
                "create_time",
                "<=",
                strtotime(input('createTimeEnd'))
            ];
        }
        
        $count = $this->model->withJoin('user', 'LEFT')
            ->where($where)
            ->count();
        
        if (! empty($page) && ! empty($limit)) {
            $list = $this->model->withJoin('user', 'LEFT')
                ->where($where)
                ->field("system_operation_record.*,system_operation_record.request_method requestMethod,user.nickname nickname ,user.username username ,system_operation_record.create_time createTime")
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
        } else {
            $list = $this->model->withJoin('user', 'LEFT')
                ->where($where)
                ->field("system_operation_record.*,system_operation_record.request_method requestMethod,user.nickname nickname ,user.username username ,system_operation_record.create_time createTime")
                ->order($this->sort)
                ->select();
        }
        
        return msg([
            'list' => $list,
            'count' => $count
        ], 0, "success");
    }
}
